local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/bs_decomposition_SS_ProdR_new_kl", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_b2* *_b3*
drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2_`var' = `var'2 - ($sdv * `var'2se)
	gen highs2_`var' = `var'2 + ($sdv * `var'2se)
	gen lows3_`var' = `var'3 - ($sdv * `var'3se)
	gen highs3_`var' = `var'3 + ($sdv * `var'3se)
}



		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		
		keep if ordering==6
		replace ordering =1
		
		generate spec = "SVA"
		
		tempfile t1
		save `t1', replace
		
clear
		
local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/bs_decomposition_SS_ProdQ_new_klme_TFPQIV", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_b2* *_b3*
drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2_`var' = `var'2 - ($sdv * `var'2se)
	gen highs2_`var' = `var'2 + ($sdv * `var'2se)
	gen lows3_`var' = `var'3 - ($sdv * `var'3se)
	gen highs3_`var' = `var'3 + ($sdv * `var'3se)
}



		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		
		keep if ordering==6
		replace ordering = 2
		
		generate spec = "GO ES"
		
		tempfile t2
		save `t2', replace
		
			
clear
		
local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/bs_decomposition_SS_ProdQ_new_klme_TFPQIV_rshare", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_b2* *_b3*
drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2_`var' = `var'2 - ($sdv * `var'2se)
	gen highs2_`var' = `var'2 + ($sdv * `var'2se)
	gen lows3_`var' = `var'3 - ($sdv * `var'3se)
	gen highs3_`var' = `var'3 + ($sdv * `var'3se)
}



		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		
		keep if ordering==6
		replace ordering = 3
		
		generate spec = "GO RS"
			
		
		append using `t1'
		append using `t2'
		
		sencode spec, gen(vars) gsort(ordering)
		gen vars1 = vars+.3
		gen vars2 = vars+.15
		
				twoway (scatter Iy2 vars, mcolor(red) msymbol(smsquare)) ///
				(rcap lows2_Iy highs2_Iy vars if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iy highs2_Iy vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iyn2 vars2, mcolor(red) msymbol(Sh)) ///
				(rcap lows2_Iyn highs2_Iyn vars2 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iyn highs2_Iyn vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)3, angle(45) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Year FE" 4 "Industry Year FE") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color)
 graph export "$figures/Input_access_comparison.png", as(png) replace width(1800) height(1000)
